package com.dhcc.sds.standard.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.dao.CdcDictionaryMapper;
import com.dhcc.cdc.busi.dao.CdcOrdmastMapper;
import com.dhcc.cdc.busi.entity.CdcDictionaryEntity;
import com.dhcc.cdc.busi.entity.CdcOrdmastEntity;
import com.dhcc.cdc.busi.service.ICdcDictionaryService;
import com.dhcc.cdc.sys.service.IDictionaryService;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.sds.standard.dao.SdsStandDicsubMapper;
import com.dhcc.sds.standard.dao.SdsStandMatchLogMapper;
import com.dhcc.sds.standard.dao.SdsStandOrderMatchMapper;
import com.dhcc.sds.standard.dto.CdcOrdmastDto;
import com.dhcc.sds.standard.entity.SdsStandDicMatchParamEntity;
import com.dhcc.sds.standard.entity.SdsStandDicsubEntity;
import com.dhcc.sds.standard.entity.SdsStandMatchLogEntity;
import com.dhcc.sds.standard.entity.SdsStandOrderMatchEntity;
import com.dhcc.sds.standard.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 标准化_医嘱对照表数据服务层接口
 *
 * @author sunyingjie
 * @ClassName: SdsStandOrderMatchServiceImpl
 * @Description: TODO
 * @date 2021-03-22 10:09:56
 */
@Service("sdsStandOrderMatchService")
public class SdsStandOrderMatchServiceImpl extends BaseServiceImpl<SdsStandOrderMatchMapper, SdsStandOrderMatchEntity> implements ISdsStandOrderMatchService {

    @Autowired
    private CdcOrdmastMapper ordmastMapper;

    @Autowired
    private SdsStandDicsubMapper standDicsubMapper;

    @Autowired
    private ISdsStandDicService sdsStandDicService;

    @Autowired
    private ISdsStandDicsubService sdsStandDicsubService;

    @Autowired
    private CdcDictionaryMapper cdcDictionaryMapper;

    @Autowired
    private SdsStandMatchLogMapper sdsStandMatchLogMapper;

    @Autowired
    private IDictionaryService dictionaryService;

    @Autowired
    private ICdcDictionaryService cdcDictionaryService;

    @Autowired
    private ISdsStandSourceService standSourceService;

    @Autowired
    private ISdsStandDicMatchParamService sdsStandDicMatchParamService;

    double all = 1;
    Double cur =(double) 0;

    @Override
    public List<SdsStandOrderMatchEntity> page(Page<SdsStandOrderMatchEntity> page, Map<String, Object> map, String compare) {
        List<SdsStandOrderMatchEntity> page1 = this.baseMapper.page(page, map);
        for (SdsStandOrderMatchEntity standOrderMatchEntity : page1) {
            if (standOrderMatchEntity.getOrdmastId() != null) {
                CdcOrdmastEntity ordmastEntity = ordmastMapper.selectById(standOrderMatchEntity.getOrdmastId());
                standOrderMatchEntity.setOrdmastDesc(ordmastEntity.getDescription());
            }

            if (standOrderMatchEntity.getDicsubId() != null) {
                SdsStandDicsubEntity dicsubEntity = standDicsubMapper.selectById(standOrderMatchEntity.getDicsubId());
                standOrderMatchEntity.setDicsubDesc(dicsubEntity.getDicDesc());
            }
        }

        List<SdsStandOrderMatchEntity> collect = new ArrayList<>();
        if ("yes".equals(compare)) {
            collect = page1.stream().filter(item -> item.getDicsubId() != null).collect(Collectors.toList());
            return collect;
        } else if ("no".equals(compare)) {
            collect = page1.stream().filter(item -> item.getDicsubId() == null).collect(Collectors.toList());
            return collect;
        } else {
            return page1;
        }
    }

    @Override
    public List<SdsStandOrderMatchEntity> list(Map<String, Object> map) {
        return this.baseMapper.list(map);
    }

    @Override
    public void sync() {
        //查询医嘱项数据
        List<CdcOrdmastEntity> list = ordmastMapper.selectList(null);
        //查询标准化_医嘱对照表数据
        List<SdsStandOrderMatchEntity> list1 = this.baseMapper.selectList(null);

        //取两个list差集
        List<CdcOrdmastEntity> collect = list.stream()
                .filter(item -> !list1.stream()
                        .map(e -> e.getOrdmastId())
                        .collect(Collectors.toList())
                        .contains(item.getId()))
                .collect(Collectors.toList());

        //插入数据

        for (CdcOrdmastEntity ordmastEntity : collect) {
            SdsStandOrderMatchEntity standOrderMatchEntity = new SdsStandOrderMatchEntity();
            standOrderMatchEntity.setOrdmastId(ordmastEntity.getId());
            this.baseMapper.insert(standOrderMatchEntity);
        }
    }

    @Override
    public void undo(String ordmastIds, Long sdsStandDicsubId, Long sdsStandDicId, String username) {


        String[] split = ordmastIds.split(",");
        for (String ordmastId : split) {
            SdsStandOrderMatchEntity standOrderMatchEntity = new SdsStandOrderMatchEntity();
            SdsStandMatchLogEntity sdsStandMatchLogEntity = new SdsStandMatchLogEntity();
            if (!StringUtils.isEmpty(sdsStandDicsubId)) {
                standOrderMatchEntity.setDicsubId(sdsStandDicsubId);
                sdsStandMatchLogEntity.setDicsubId(sdsStandDicsubId);
            } else if (!StringUtils.isEmpty(sdsStandDicId)) {
                standOrderMatchEntity.setDicId(sdsStandDicId);
                sdsStandMatchLogEntity.setDicId(sdsStandDicId);
            }

            standOrderMatchEntity.setOrdmastId(Long.valueOf(ordmastId));
            sdsStandMatchLogEntity.setOrdmastId(Long.valueOf(ordmastId));
            sdsStandMatchLogEntity.setOperType(2);
            sdsStandMatchLogEntity.setMatchType("医嘱对照");
            sdsStandMatchLogEntity.setOperUser(username);
            SdsStandOrderMatchEntity standOrderMatchEntity1 = this.baseMapper.selectOne(standOrderMatchEntity);
            this.baseMapper.deleteById(standOrderMatchEntity1);
            //更新日志
            sdsStandMatchLogEntity.setOperTime(new Date());
            sdsStandMatchLogMapper.insert(sdsStandMatchLogEntity);
        }

    }

    @Override
    public void compare(Long sdsStandDicsubId, String ordmastIds, Long sdsStandDicId, String username) {
		/*String[] split = ordmastIds.split(",");
		for (int i = 0; i < split.length; i++){
			if (split[i] != null || !"".equals(split[i])){
				SdsStandOrderMatchEntity standOrderMatchEntity = new SdsStandOrderMatchEntity();
				if (!StringUtils.isEmpty(sdsStandDicsubId)){
					standOrderMatchEntity.setDicsubId(sdsStandDicsubId);
				}else if (!StringUtils.isEmpty(sdsStandDicId)){
					standOrderMatchEntity.setDicId(sdsStandDicId);
				}
				standOrderMatchEntity.setOrdmastId(Long.parseLong(split[i]));
				SdsStandOrderMatchEntity standOrderMatchEntity1 = this.baseMapper.selectOne(standOrderMatchEntity);
				if (standOrderMatchEntity1 == null){
					//insert
					this.baseMapper.insert(standOrderMatchEntity);
				}
			}
		}*/


        String[] split = ordmastIds.split(",");
        for (String ordmastId : split) {
            SdsStandOrderMatchEntity standOrderMatchEntity = new SdsStandOrderMatchEntity();
            SdsStandMatchLogEntity sdsStandMatchLogEntity = new SdsStandMatchLogEntity();

            if (!StringUtils.isEmpty(sdsStandDicsubId)) {
                standOrderMatchEntity.setDicsubId(sdsStandDicsubId);
                sdsStandMatchLogEntity.setDicsubId(sdsStandDicsubId);
            } else if (!StringUtils.isEmpty(sdsStandDicId)) {
                standOrderMatchEntity.setDicId(sdsStandDicId);
                sdsStandMatchLogEntity.setDicId(sdsStandDicId);
            }

            standOrderMatchEntity.setOrdmastId(Long.valueOf(ordmastId));
            standOrderMatchEntity.setMatchType("H");
            sdsStandMatchLogEntity.setOrdmastId(Long.valueOf(ordmastId));
            sdsStandMatchLogEntity.setOperType(1);
            sdsStandMatchLogEntity.setMatchType("医嘱对照");
            sdsStandMatchLogEntity.setOperUser(username);
            SdsStandOrderMatchEntity standOrderMatchEntity1 = this.baseMapper.selectOne(standOrderMatchEntity);
            if (standOrderMatchEntity1 == null) {
                //insert
                this.baseMapper.insert(standOrderMatchEntity);
                sdsStandMatchLogEntity.setOperTime(new Date());
                //更新日志
                sdsStandMatchLogMapper.insert(sdsStandMatchLogEntity);
            }
        }
    }


    @Override
    public void autoCompare() {
        //医嘱对照数据源：非抗菌药物、抗菌药物、处置操作
        List<SdsStandDicMatchParamEntity> standDicMatchEntities = sdsStandDicMatchParamService.getDicBySource(Arrays.asList("非抗菌药物", "抗菌药物", "处置操作"));
        List<SdsStandDicMatchParamEntity> standDicsubMatchEntities = sdsStandDicMatchParamService.getDicsubBySource(Arrays.asList("非抗菌药物", "抗菌药物", "处置操作"));
        //List<SdsStandDicMatchEntity> standDicMatchEntities = sdsStandDicMatchService.getBySource(Arrays.asList("非抗菌药物", "抗菌药物", "处置操作"));
        //不对照护理医嘱、不对照无效医嘱
        CdcDictionaryEntity cdcDictionaryEntity = cdcDictionaryService.queryByTypeCodeAndCode("OrdData", "bus_order_cate", "护理");
        EntityWrapper wrapper = new EntityWrapper<CdcOrdmastEntity>();
        wrapper.eq("is_active",1).ne("bus_order_cate_id", cdcDictionaryEntity.getId());
        List<CdcOrdmastEntity> cdcOrdmastEntities = ordmastMapper.selectList(wrapper);
        List<CdcOrdmastDto> cdcOrdmastDtos = cdcOrdmastEntities.stream().map(t ->{
            CdcOrdmastDto cdcOrdmastDto = new CdcOrdmastDto();
            BeanUtils.copyProperties(t, cdcOrdmastDto);
            return cdcOrdmastDto;
        }).collect(Collectors.toList());

        Map<Long, List<SdsStandDicMatchParamEntity>> dicMatchMap = new HashMap<>();
        Map<Long, List<SdsStandDicMatchParamEntity>> dicsubMatchMap = new HashMap<>();

        //处理dic数据
        for (SdsStandDicMatchParamEntity standDicMatchEntity : standDicMatchEntities){
            if (CommonUtil.isEmpty(dicMatchMap.get(standDicMatchEntity.getDicId()))){
                List<SdsStandDicMatchParamEntity> dicMatchEntities = new ArrayList<>();
                dicMatchEntities.add(standDicMatchEntity);
                dicMatchMap.put(standDicMatchEntity.getDicId(), dicMatchEntities);
            }else {
                List<SdsStandDicMatchParamEntity> dicMatchEntities = dicMatchMap.get(standDicMatchEntity.getDicId());
                dicMatchEntities.add(standDicMatchEntity);
            }
        }
        //处理dicsub数据
        /*for (SdsStandDicMatchParamEntity standDicMatchEntity : standDicsubMatchEntities){
            if (CommonUtil.isEmpty(dicsubMatchMap.get(standDicMatchEntity.getDicsubId()))){
                List<SdsStandDicMatchEntity> dicMatchEntities = new ArrayList<>();
                dicMatchEntities.add(standDicMatchEntity);
                dicsubMatchMap.put(standDicMatchEntity.getDicsubId(), dicMatchEntities);
            }else {
                List<SdsStandDicMatchEntity> dicMatchEntities = dicsubMatchMap.get(standDicMatchEntity.getDicsubId());
                dicMatchEntities.add(standDicMatchEntity);
            }
        }*/
        List<SdsStandOrderMatchEntity> sdsStandOrderMatchEntities = new ArrayList<>();
        //标准字典与医嘱项对照
        Map<Long, List<CdcOrdmastDto>> ordmastMap = new HashMap<>();
        for (Long dicId : dicMatchMap.keySet()){
            List<SdsStandDicMatchParamEntity> dicMatchEntities = dicMatchMap.get(dicId);
            String filterTxt = "";
            List<CdcOrdmastDto> matchOrdmast = new ArrayList<>();
            a:for (SdsStandDicMatchParamEntity standDicMatchEntity : dicMatchEntities){
                String filterType = standDicMatchEntity.getFilterType();
                String matchTxt = standDicMatchEntity.getCompareContent();
                if (Objects.equals("O", filterType)){
                    filterTxt = filterTxt +  ":" + matchTxt + ",";
                    continue a;
                }
                for (CdcOrdmastDto cdcOrdmastDto : cdcOrdmastDtos){
                    String drugCommonName = cdcOrdmastDto.getDrugCommonName();
                    String description = cdcOrdmastDto.getDescription();
                    ////处理并且关系的关键字:&分隔的关键字同时满足
                    boolean checkFlg = true;
                    if (matchTxt.contains("&")){
                        String[] split = matchTxt.split("&");
                        b:for (String s : split){
                            if (StringUtils.isEmpty(drugCommonName)){
                                if (!description.contains(s)){
                                    checkFlg = false;
                                    break b;
                                }
                            }else {
                                if (!description.contains(s) && !drugCommonName.contains(s)){
                                    checkFlg = false;
                                    break b;
                                }
                            }
                        }
                        if (checkFlg){
                            cdcOrdmastDto.setMatchType("K");
                            matchOrdmast.add(cdcOrdmastDto);
                            ordmastMap.put(dicId, matchOrdmast);
                        }
                    }else {
                        if (StringUtils.isEmpty(drugCommonName)){
                            if (description.contains(matchTxt)){
                                cdcOrdmastDto.setMatchType("K");
                                matchOrdmast.add(cdcOrdmastDto);
                                ordmastMap.put(dicId, matchOrdmast);
                            }
                        }else {
                            if (description.contains(matchTxt) || drugCommonName.contains(matchTxt)){
                                cdcOrdmastDto.setMatchType("K");
                                matchOrdmast.add(cdcOrdmastDto);
                                ordmastMap.put(dicId, matchOrdmast);
                            }
                        }
                    }
                }
            }
            //排除
            if (!StringUtils.isEmpty(filterTxt)){
                String[] split = filterTxt.split(",");
                List<CdcOrdmastDto> ordmastEntities = ordmastMap.get(dicId);
                Iterator<CdcOrdmastDto> it = ordmastEntities.iterator();
                while (it.hasNext()){
                    CdcOrdmastDto ordmastEntity = it.next();
                    for (String s : split){
                        String[] split1 = s.split(":");
                        if (StringUtils.isEmpty(ordmastEntity.getDrugCommonName())){
                            if (ordmastEntity.getDescription().contains(split1[1])){
                                it.remove();
                            }
                        }else {
                            if (ordmastEntity.getDescription().contains(split1[1]) || ordmastEntity.getDrugCommonName().contains(split1[1])){
                                it.remove();
                            }
                        }

                    }
                }
                ordmastMap.put(dicId, ordmastEntities);
            }

        }
        for (Long dicId : ordmastMap.keySet()){
            List<CdcOrdmastDto> ordmastEntities = ordmastMap.get(dicId);
            for (CdcOrdmastDto cdcOrdmastEntity : ordmastEntities){
                SdsStandOrderMatchEntity standOrderMatchEntity = new SdsStandOrderMatchEntity();
                standOrderMatchEntity.setDicId(dicId);
                standOrderMatchEntity.setOrdmastId(cdcOrdmastEntity.getId());
                SdsStandOrderMatchEntity standOrderMatchEntity1 = this.baseMapper.selectOne(standOrderMatchEntity);
                if (StringUtils.isEmpty(standOrderMatchEntity1)) {
                    standOrderMatchEntity.setMatchType(cdcOrdmastEntity.getMatchType());
                    standOrderMatchEntity.setMatchDate(new Date());
                    sdsStandOrderMatchEntities.add(standOrderMatchEntity);
                }
            }
        }


        //标准字典子项与医嘱项对照
        Map<Long, List<CdcOrdmastDto>> ordmastMap2 = new HashMap<>();
        for (Long dicsubId : dicsubMatchMap.keySet()){
            List<SdsStandDicMatchParamEntity> dicMatchEntities = dicsubMatchMap.get(dicsubId);
            String filterTxt = "";
            List<CdcOrdmastDto> matchOrdmast = new ArrayList<>();
            a:for (SdsStandDicMatchParamEntity standDicMatchEntity : dicMatchEntities){
                String filterType = standDicMatchEntity.getFilterType();
                String matchTxt = standDicMatchEntity.getCompareContent();
                if (Objects.equals("O", filterType)){
                    filterTxt = filterTxt  + ":" + matchTxt + ",";
                    continue a;
                }
                for (CdcOrdmastDto ordmastEntity : cdcOrdmastDtos){
                    String drugCommonName = ordmastEntity.getDrugCommonName();
                    String description = ordmastEntity.getDescription();
                    ////处理并且关系的关键字:&分隔的关键字同时满足
                    boolean checkFlg = true;
                    if (matchTxt.contains("&")){
                        String[] split = matchTxt.split("&");
                        b:for (String s : split){
                            if (StringUtils.isEmpty(drugCommonName)){
                                if (!description.contains(s)){
                                    checkFlg = false;
                                    break b;
                                }
                            }else {
                                if (!description.contains(s) && !drugCommonName.contains(s)){
                                    checkFlg = false;
                                    break b;
                                }
                            }
                        }
                        if (checkFlg){
                            ordmastEntity.setMatchType("K");
                            matchOrdmast.add(ordmastEntity);
                            ordmastMap2.put(dicsubId, matchOrdmast);
                        }
                    }else {
                        if (StringUtils.isEmpty(drugCommonName)){
                            if (description.contains(matchTxt)){
                                ordmastEntity.setMatchType("K");
                                matchOrdmast.add(ordmastEntity);
                                ordmastMap2.put(dicsubId, matchOrdmast);
                            }
                        }else {
                            if (description.contains(matchTxt) || drugCommonName.contains(matchTxt)){
                                ordmastEntity.setMatchType("K");
                                matchOrdmast.add(ordmastEntity);
                                ordmastMap2.put(dicsubId, matchOrdmast);
                            }
                        }
                    }
                }
            }
            //排除
            if (!StringUtils.isEmpty(filterTxt)){
                String[] split = filterTxt.split(",");
                List<CdcOrdmastDto> ordmastEntities = ordmastMap2.get(dicsubId);
                Iterator<CdcOrdmastDto> it = ordmastEntities.iterator();
                while (it.hasNext()){
                    CdcOrdmastDto ordmastEntity = it.next();
                    for (String s : split){
                        String[] split1 = s.split(":");
                        if (StringUtils.isEmpty(ordmastEntity.getDrugCommonName())){
                            if (ordmastEntity.getDescription().contains(split1[1])){
                                it.remove();
                            }
                        }else {
                            if (ordmastEntity.getDescription().contains(split1[1]) || ordmastEntity.getDrugCommonName().contains(split1[1])){
                                it.remove();
                            }
                        }

                    }
                }
                ordmastMap2.put(dicsubId, ordmastEntities);
            }

        }
        for (Long dicsubId : ordmastMap2.keySet()){
            List<CdcOrdmastDto> ordmastEntities = ordmastMap2.get(dicsubId);
            for (CdcOrdmastDto cdcOrdmastEntity : ordmastEntities){
                SdsStandOrderMatchEntity standOrderMatchEntity = new SdsStandOrderMatchEntity();
                standOrderMatchEntity.setDicsubId(dicsubId);
                standOrderMatchEntity.setOrdmastId(cdcOrdmastEntity.getId());
                SdsStandOrderMatchEntity standOrderMatchEntity1 = this.baseMapper.selectOne(standOrderMatchEntity);
                if (StringUtils.isEmpty(standOrderMatchEntity1)) {
                    standOrderMatchEntity.setMatchType(cdcOrdmastEntity.getMatchType());
                    standOrderMatchEntity.setMatchDate(new Date());
                    sdsStandOrderMatchEntities.add(standOrderMatchEntity);
                }
            }
        }

        this.baseMapper.insertBatch(sdsStandOrderMatchEntities);
    }


    private void updateOrderMatch(Long dicId, Long dicsubId, String matchType, Long ordmastId) {
        if (null != dicId) {
            SdsStandOrderMatchEntity standOrderMatchEntity = new SdsStandOrderMatchEntity();
            standOrderMatchEntity.setDicId(dicId);
            standOrderMatchEntity.setOrdmastId(ordmastId);
            SdsStandOrderMatchEntity standOrderMatchEntity1 = this.baseMapper.selectOne(standOrderMatchEntity);
            if (StringUtils.isEmpty(standOrderMatchEntity1)) {
                standOrderMatchEntity.setMatchType(matchType);
                this.baseMapper.insert(standOrderMatchEntity);
            }
        } else if (null != dicsubId) {
            SdsStandOrderMatchEntity standOrderMatchEntity = new SdsStandOrderMatchEntity();
            standOrderMatchEntity.setDicsubId(dicsubId);
            standOrderMatchEntity.setOrdmastId(ordmastId);
            SdsStandOrderMatchEntity standOrderMatchEntity1 = this.baseMapper.selectOne(standOrderMatchEntity);
            if (StringUtils.isEmpty(standOrderMatchEntity1)) {
                standOrderMatchEntity.setMatchType(matchType);
                this.baseMapper.insert(standOrderMatchEntity);
            }
        }
    }

    private void updateDicMactch(Map<Long, List<CdcOrdmastDto>> ordmastMap){
        for (Long dicId : ordmastMap.keySet()){
            List<CdcOrdmastDto> ordmastEntities = ordmastMap.get(dicId);
            for (CdcOrdmastDto cdcOrdmastEntity : ordmastEntities){
                this.updateOrderMatch(dicId, null, cdcOrdmastEntity.getMatchType(), cdcOrdmastEntity.getId());
            }
        }
    }

    private void updateDicsubMactch(Map<Long, List<CdcOrdmastDto>> ordmastMap2){
        for (Long dicsubId : ordmastMap2.keySet()){
            List<CdcOrdmastDto> ordmastEntities = ordmastMap2.get(dicsubId);
            for (CdcOrdmastDto cdcOrdmastEntity : ordmastEntities){
                this.updateOrderMatch(null, dicsubId, cdcOrdmastEntity.getMatchType(), cdcOrdmastEntity.getId());
            }
        }
    }

    @Override
    public List<CdcOrdmastDto> pageOrdmast(Page<CdcOrdmastEntity> page, Map<String, Object> params, String compare) {
        Object dicSubId = params.get("dicSubId");
        Object dicId = params.get("dicId");
        if (StringUtils.isEmpty(dicSubId) && StringUtils.isEmpty(dicId)) {
            return null;
        }


        //String typeCode = dictionaryService.getMatchCtDicCode("SDS", "SDMatchCdcDic", "WestMedicine");
        //String dicTypeCode = dictionaryService.getMatchCtDic("SDS", "SDMatchCdcDic", "bus_order_cate");
        //CdcDictionaryEntity dictionaryEntity = cdcDictionaryService.queryByTypeCodeAndCode("OrdData", "bus_order_cate", typeCode);
        //过滤医嘱为西药的
        CdcDictionaryEntity dictionaryEntity = dictionaryService.getMatchCtDic("SDS", "SDMatchCdcDic", "WestMedicine");
        //add 过滤医嘱为处置治疗的
        CdcDictionaryEntity dictionaryEntity2 = dictionaryService.getMatchCtDic("SDS", "SDMatchCdcDic", "DisposalTreat");
		/*if (!StringUtils.isEmpty(dictionaryEntity)){
			params.put("busOrderCateId",dictionaryEntity.getId());
		}*/

        List<CdcOrdmastEntity> page1 = new ArrayList<>();
        List<CdcOrdmastDto> cdcOrdmastDtoList = CollUtil.newArrayList();
        List<SdsStandOrderMatchEntity> sdsStandOrderMatchEntities = new ArrayList<>();
        String sourceName;
        if (!StringUtils.isEmpty(dicSubId)) {
            String s = String.valueOf(dicSubId);
            sourceName = standSourceService.getSourceNameByDicSub(Long.valueOf(s));
            params.put("dicsubId", s);
            sdsStandOrderMatchEntities = this.baseMapper.selectList(
                    new EntityWrapper<SdsStandOrderMatchEntity>()
                            .eq("dicsub_id", s)
            );
        } else {
            String s = String.valueOf(dicId);
            sourceName = standSourceService.getSourceNameByDic(Long.valueOf(s));
            params.put("dicId", s);
            sdsStandOrderMatchEntities = this.baseMapper.selectList(
                    new EntityWrapper<SdsStandOrderMatchEntity>()
                            .eq("dic_id", s)
            );
        }

        if (Objects.equals("非抗菌药物", sourceName) || Objects.equals("抗菌药物", sourceName)) {
            params.put("busOrderCateId", dictionaryEntity.getId());
        } else if (Objects.equals("处置操作", sourceName)) {
            params.put("busOrderCateId", dictionaryEntity2.getId());
        }


        if (Objects.equals("all", compare)) {
            page1 = ordmastMapper.page(page, params);
        } else {
            List<Long> matchOrdmastIds = this.baseMapper.selectNatchOrdmastIds(params);
            page1 = ordmastMapper.pageExcludeMatch(page, params, matchOrdmastIds);
        }

        for (CdcOrdmastEntity cdcOrdmastEntity : page1) {
            CdcOrdmastDto cdcOrdmastDto = new CdcOrdmastDto();
            BeanUtil.copyProperties(cdcOrdmastEntity, cdcOrdmastDto);
            CdcDictionaryEntity cdcDictionaryEntity = cdcDictionaryMapper.selectById(cdcOrdmastDto.getBusOrderTypeId());
            cdcOrdmastDto.setOrderTypeDesc(cdcDictionaryEntity.getDescription());
            cdcOrdmastDtoList.add(cdcOrdmastDto);
        }

        //关联的数据进行勾选
        for (CdcOrdmastDto cdcOrdmastDto : cdcOrdmastDtoList) {
            for (SdsStandOrderMatchEntity standOrderMatchEntity : sdsStandOrderMatchEntities) {
                if (cdcOrdmastDto.getId().equals(standOrderMatchEntity.getOrdmastId())) {
                    cdcOrdmastDto.setIsCheck(true);
                }
            }
        }

        return cdcOrdmastDtoList;
		/*List<CdcOrdmastEntity> page1 = ordmastMapper.page(page, params);
		//List<CdcOrdmastEntity> page1 = ordmastMapper.list(params);
		List<CdcOrdmastDto> cdcOrdmastDtoList = CollUtil.newArrayList();
		for (CdcOrdmastEntity cdcOrdmastEntity : page1){
			CdcOrdmastDto cdcOrdmastDto = new CdcOrdmastDto();
			BeanUtil.copyProperties(cdcOrdmastEntity, cdcOrdmastDto);
			CdcDictionaryEntity cdcDictionaryEntity = cdcDictionaryMapper.selectById(cdcOrdmastDto.getBusOrderTypeId());
			cdcOrdmastDto.setOrderTypeDesc(cdcDictionaryEntity.getDescription());
			cdcOrdmastDtoList.add(cdcOrdmastDto);
		}

		if ((dicSubId != null) && (!"".equals(dicSubId))){
			//点击标准化字典子项
			String s = String.valueOf(dicSubId);
			List<SdsStandOrderMatchEntity> sdsStandOrderMatchEntities = this.baseMapper.selectList(
					new EntityWrapper<SdsStandOrderMatchEntity>()
							.eq("dicsub_id", s)
			);
			//关联的数据进行勾选
			for (CdcOrdmastDto cdcOrdmastDto : cdcOrdmastDtoList){
				for (SdsStandOrderMatchEntity standOrderMatchEntity : sdsStandOrderMatchEntities){
					if (cdcOrdmastDto.getId().equals(standOrderMatchEntity.getOrdmastId())){
						cdcOrdmastDto.setIsCheck(true);
					}
				}
			}

		}else if ((dicId != null) && (!"".equals(dicId))){
			//点击标准化字典
			String s = String.valueOf(dicId);
			List<SdsStandOrderMatchEntity> sdsStandOrderMatchEntities = this.baseMapper.selectList(
					new EntityWrapper<SdsStandOrderMatchEntity>()
							.eq("dic_id", s)
			);
			//关联的数据进行勾选
			for (CdcOrdmastDto cdcOrdmastDto : cdcOrdmastDtoList){
				for (SdsStandOrderMatchEntity standOrderMatchEntity : sdsStandOrderMatchEntities){
					if (cdcOrdmastDto.getId().equals(standOrderMatchEntity.getOrdmastId())){
						cdcOrdmastDto.setIsCheck(true);
					}
				}
			}
		} else {
			//不点击默认查询所有
			return cdcOrdmastDtoList;
		}*/

        //已对照-yes，未对照-no，全部
		/*List<CdcOrdmastDto> collect = new ArrayList<>();
		if("yes".equals(compare)){
			collect = cdcOrdmastDtoList.stream().filter(item -> (item.getIsCheck() != null && item.getIsCheck() == true)).collect(Collectors.toList());
			return collect;
		}else if("no".equals(compare)){
			collect = cdcOrdmastDtoList.stream().filter(item -> (item.getIsCheck() == null || "".equals(item.getIsCheck()))).collect(Collectors.toList());
			return collect;
		}else {
			return cdcOrdmastDtoList;
		}*/
		/*List<CdcOrdmastDto> collect = new ArrayList<>();
		if (Objects.equals("noMatch", compare)){
			collect = cdcOrdmastDtoList.stream().filter(item -> (item.getIsCheck() == null || "".equals(item.getIsCheck()))).collect(Collectors.toList());
			return collect;
		}else {
			return cdcOrdmastDtoList;
		}*/

    }

    @Override
    public List<CdcOrdmastDto> pageMatchOrdmast(Page<SdsStandOrderMatchEntity> page, Map<String, Object> params, String compare) {
        Object dicSubId = params.get("dicSubId");
        Object dicId = params.get("dicId");
        if (StringUtils.isEmpty(dicSubId) && StringUtils.isEmpty(dicId)) {
            return null;
        }

        List<CdcOrdmastDto> cdcOrdmastDtoList = CollUtil.newArrayList();
        if (!StringUtils.isEmpty(dicSubId)) {
            //点击标准化字典子项
            String s = String.valueOf(dicSubId);
            params.put("dicsubId", s);
        } else if (!StringUtils.isEmpty(dicId)) {
            String s = String.valueOf(dicId);
            params.put("dicId", s);
        }
        List<SdsStandOrderMatchEntity> page1 = this.baseMapper.page(page, params);

        if (Objects.equals("match", compare)) {
            //默认查询已对照医嘱，未对照返回空
            if (page1.size() != 0) {
                for (SdsStandOrderMatchEntity standOrderMatchEntity : page1) {
                    CdcOrdmastDto cdcOrdmastDto = new CdcOrdmastDto();
                    CdcOrdmastEntity ordmastEntity = ordmastMapper.selectById(standOrderMatchEntity.getOrdmastId());
                    BeanUtil.copyProperties(ordmastEntity, cdcOrdmastDto);
                    CdcDictionaryEntity cdcDictionaryEntity = cdcDictionaryMapper.selectById(cdcOrdmastDto.getBusOrderTypeId());
                    if (cdcDictionaryEntity!=null) {
                        cdcOrdmastDto.setOrderTypeDesc(cdcDictionaryEntity.getDescription());
                    }
                    cdcOrdmastDto.setIsCheck(true);
                    cdcOrdmastDtoList.add(cdcOrdmastDto);
                }
            }
        }

        return cdcOrdmastDtoList;
    }

    @Override
    public List<Long> selectMatchDicsub() {
        return this.baseMapper.selectMatchDicsub();
    }


    public class AnalysisData extends Thread{
        private Map<Long, List<CdcOrdmastDto>> ordmastMap;
        private Map<Long, List<CdcOrdmastDto>> ordmastMap2;
        public AnalysisData(Map<Long, List<CdcOrdmastDto>> ordmastMap, Map<Long, List<CdcOrdmastDto>> ordmastMap2){
            this.ordmastMap = ordmastMap;
            this.ordmastMap2 = ordmastMap2;
        }
        public void run() {
            try {
                //消费
                AnalysisDataMain(ordmastMap, ordmastMap2);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void AnalysisDataMain(Map<Long, List<CdcOrdmastDto>> ordmastMap, Map<Long, List<CdcOrdmastDto>> ordmastMap2) throws Exception {
        //checkSDByAdm(patientId);
        if (null == ordmastMap){
            this.updateDicsubMactch(ordmastMap2);
        }else {
            this.updateDicMactch(ordmastMap);
        }
        //这个时候处理完一个病例 多线程加锁
        synchronized (cur) {
            cur++;
        }
    }

}